#### time add
# "2021-08-09" add 1 month = "2021-09-09"

get_time_element <- function(date = "0000-00-00"){
  date <- as.character(as.Date(date))
  year <- as.numeric(substr(date, start = 1, stop = 4))
  month <- as.numeric(substr(date, start = 6, stop = 7))
  day <- as.numeric(substr(date, start = 9, stop = 10))
  return(c(year, month, day))
}

add_year <- function(date = "0000-00-00", add_unit = 1){
  date_vec <- get_time_element(date)
  date_vec[1] <- date_vec[1] + add_unit
  return(as.character(as.Date(paste(date_vec[1],"-",date_vec[2],"-",date_vec[3], sep = ""))))
}

add_month <- function(date = "0000-00-00", add_unit = 1){
  date_vec <- get_time_element(date)
  total_month <- date_vec[2] + add_unit
  year_add <- (total_month - (total_month%%12))/12
  month_new <- (total_month%%12)
  if(month_new == 0){
    month_new <- 12
    year_add <- year_add - 1
  }
  
  date_vec[1] <- date_vec[1] + year_add
  date_vec[2] <- month_new
  return(as.character(as.Date(paste(date_vec[1],"-",date_vec[2],"-",date_vec[3], sep = ""))))
}

add_day <- function(date = "0000-00-00", add_unit = 1){
  date_day <- as.Date(date)
  date_day <- date_day + add_unit
  return(as.character(date_day))
}


#### Test Area ####
# get_time_element(date = "2021/09/07")
# get_time_element(date = "2021-09-07")

# add_year(date = "2013-08-09", add_unit = 5)
# add_year(date = "2013/08/09")

# add_month(date = "2021-07-20", add_unit = 3)
# add_month(date = "2021-07-20", add_unit = 9)
# add_month(date = "2021-07-20", add_unit = 50)

# add_day("2020-10-01", add_unit = 1000)
